﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gpsys.Entity.MSG;
using Gpsys.Web.Business;
using System.Web.Mvc;

namespace Gpsys.Web.Models
{
    public class UserEntityModel
    {
        public User User { get; set; }
    }

    public class UserModel
    {
        public bool InsertUser(User user)
        {
            UserBusiness bs = new UserBusiness();

            return bs.InsertUser(user);
        }

        public bool UpdateUser(User user)
        {
            UserBusiness bs = new UserBusiness();

            return bs.UpdateUser(user);
        }

        public User GetUser(int Id)
        {
            UserBusiness bs = new UserBusiness();

            return bs.GetUser(Id);
        }

        public void DeleteUser(int Id)
        {
            UserBusiness bs = new UserBusiness();

            bs.DeleteUser(Id);
        }

        private List<User> m_users;

        public List<User> Users
        {
            get
            {
                if (m_users == null)
                {
                    UserBusiness bs = new UserBusiness();
                    m_users = bs.GetUsers();
                }
                return m_users;
            }
        }

        public void ValidateUser(ModelStateDictionary modelState, UserEntityModel model, bool isCreate)
        {
            if (isCreate && string.IsNullOrEmpty(model.User.UserName))
                modelState.AddModelError("User.UserName", "UserName could not be empty.");

            if (string.IsNullOrEmpty(model.User.Name))
                modelState.AddModelError("User.Name", "Name could not be empty.");

            UserBusiness bs = new UserBusiness();
            if (bs.CheckCodeUsed(model.User))
            {
                modelState.AddModelError("User.UserName", "UserName is already been used.");
            }
        }
    }
}